package com.alibaba.webx.tutorial1.app1.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.webx.tutorial1.app1.dao.RoleDAO;
import com.alibaba.webx.tutorial1.app1.service.RoleService;
import com.alibaba.webx.tutorial1.app1.vo.Role;

@Service
public class RoleServiceImpl implements RoleService {
	
	static Logger log = LoggerFactory.getLogger(RoleServiceImpl.class);
	
	private RoleDAO roleDao;
	
	public void setRoleDao(RoleDAO roleDao) {
		this.roleDao = roleDao;
	}
	
	@Override
	@Transactional(readOnly = true)
	public int countAll() {
        return this.roleDao.countAll();
    }

	@Override
	@Transactional(readOnly = true)
	public Role getRole(Integer id) {
		return roleDao.selectByPrimaryKey(id);
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Boolean deleteRole(Integer roleId) throws Exception {
		try {
			return (boolean) (roleDao.deleteByPrimaryKey(roleId) > 0);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Integer addRole(String roleName) throws Exception {
		try {
			Date currentTime = new Date();
			Role role = new Role();
			role.setRoleName(roleName);
			role.setCreateTime(currentTime);
			role.setUpdateTime(currentTime);
			Integer xInteger=  roleDao.insert(role);
			return xInteger;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public Boolean updateRole(Role role) throws Exception {
		try {
			return (boolean) (roleDao.updateByPrimaryKey(role) > 0);
		} catch (Exception e) {
			throw e;
		}
	}
}
